package dianmobile.eistarandroid.network.request;

import android.content.Context;

import com.android.volley.Response;

import org.xmlpull.v1.XmlPullParser;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import dianmobile.eistarandroid.beans.PostsIntroduction;
import dianmobile.eistarandroid.utils.ConstantsData;
import dianmobile.eistarandroid.utils.HandleString;

/**
 * Created by YY on 2015/9/5.
 */
public class BoardContentRequest extends ByhhAndroidRequest {


    
    public BoardContentRequest(int requestMethod, String url, Map<String, String> params,
                                RequestCallback callback, Response.ErrorListener listener){
        super(requestMethod, url, params, callback, listener);
    }

    public static void execute(Context context, Map<String, String> params, RequestCallback requestCallback) {
        executeSuper(context, RequestType.BOARD_CONTENT, params, requestCallback);
    }
    @Override
    protected void parseXmlToMap(XmlPullParser parser, Map<String, Object> result) {

        final String TAG_TITLE = "title";
        final String TAG_AUTHOR = "author";
        final String TAG_DATE = "date";
        final String TAG_FILENAME = "filename";
        final String TAG_NAME = "name";
        final String TAG_RENUM = "reNum";
        final String TAG_SUMMARY = "summary";
        final String TAG_FLAG = "flag";


        List<PostsIntroduction> contentList = new ArrayList<>();
        PostsIntroduction postsIntroduction = null;

        try {
            int event = parser.getEventType();
            while (event != XmlPullParser.END_DOCUMENT) {
                switch (event) {
                    case XmlPullParser.START_DOCUMENT:
                        break;
                    case XmlPullParser.START_TAG:
                        String tagName = parser.getName();
                        if(tagName.equals(TAG_TITLE)) {
                            if (Integer.parseInt(parser.getAttributeValue(null, "istop")) == 0) {
                                postsIntroduction = new PostsIntroduction();
                            }
                        } else if (postsIntroduction != null && tagName.equals(TAG_AUTHOR)) {
                            postsIntroduction.setAuthor(parser.nextText());
                        } else if (postsIntroduction != null && tagName.equals(TAG_DATE)) {
                            postsIntroduction.setPostTime(
                                    HandleString.getTimeDifference(parser.nextText()));
                        } else if (postsIntroduction != null && tagName.equals(TAG_FILENAME)) {
                            postsIntroduction.setFile(parser.nextText());
                        } else if (postsIntroduction != null && tagName.equals(TAG_NAME)) {
                            postsIntroduction.setTitle(parser.nextText());
                        } else if (postsIntroduction != null && tagName.equals(TAG_RENUM)) {
                            postsIntroduction.setReplyCount(parser.nextText());
                        } else if (postsIntroduction != null && tagName.equals(TAG_SUMMARY)) {
                            postsIntroduction.setSummary(HandleString.handleArticleSummary(parser.nextText()));
                        } else if (postsIntroduction != null && tagName.equals(TAG_FLAG)) {
                            if(parser.nextText().contains("N")){
                                postsIntroduction.setRead(false);
                            } else{
                                postsIntroduction.setRead(true);
                            }
                        }
                        break;
                    case XmlPullParser.END_TAG:
                        String endTag = parser.getName();
                        if(postsIntroduction!=null && endTag.equals(TAG_TITLE)) {
                            contentList.add(postsIntroduction);
                            postsIntroduction = null;
                        }
                        break;
                    default:
                        break;
                }
                event = parser.next();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        result.put(ConstantsData.ARTICLE_LIST, contentList);
    }
}
